package com.itheima.store.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.store.domain.Dept;
import com.itheima.store.domain.Order;
import com.itheima.store.domain.OrderItem;
import com.itheima.store.domain.PageBean;
import com.itheima.store.domain.Product;
import com.itheima.store.service.DeptManagerService;
import com.itheima.store.service.OrderService;
import com.itheima.store.service.ProductService;
import com.itheima.store.utils.BaseServlet;
import com.itheima.store.utils.BeanFactory;
import com.itheima.store.utils.UUIDUtils;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

/**
 * Servlet implementation class AdminOrderServlet
 */
public class DeptManagerServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	public String findAllDept(HttpServletRequest req ,HttpServletResponse resp) {
		try {
		//获取参数state
		//String state = req.getParameter("state");
		DeptManagerService deptManagerService =(DeptManagerService)BeanFactory.getBean("deptManagerService");
		List<Dept> list =null;
		list = deptManagerService.findAllDept();
			
		//2.数据存放到request域 
		req.setAttribute("list", list);
		//3.转发到显示的页面 admin/order/list.jsp
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "admin/dpmanager/dept_list.jsp";
		
	}
	
	public String saveDept(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String dept_name  = request.getParameter("deptname");
		DeptManagerService deptManagerService =(DeptManagerService)BeanFactory.getBean("deptManagerService");
		String dept_id = UUIDUtils.getUUID();
		Dept dept = new Dept();
		dept.setDept_id(dept_id);
		dept.setDept_name(dept_name);
		
		try {
			deptManagerService.savesaveDept(dept);
			response.sendRedirect(request.getContextPath()+"/DeptManagerServlet?method=findAllDept");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String queryDept(HttpServletRequest req,HttpServletResponse resp){
		try{
			// 接收参数:
			String dept_id = req.getParameter("dept_id");
			// 调用业务层:
			DeptManagerService deptManagerService =(DeptManagerService)BeanFactory.getBean("deptManagerService");
			Dept dept = deptManagerService.queryDept(dept_id);
			
			req.setAttribute("dept", dept);
			return "admin/dpmanager/dept_edit.jsp";
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
		
	}
	
	
	public String updateDept(HttpServletRequest request,HttpServletResponse response){
		try{
			// 接收参数:
			String dept_id = request.getParameter("dept_id");
			String dept_name = request.getParameter("dept_name");
			Dept dept = new Dept();
			dept.setDept_id(dept_id);
			dept.setDept_name(dept_name);
			// 调用业务层:
			DeptManagerService deptManagerService =(DeptManagerService)BeanFactory.getBean("deptManagerService");
			deptManagerService.updateDept(dept);
			
			response.sendRedirect(request.getContextPath()+"/DeptManagerServlet?method=findAllDept");
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
		return null;
	}
	
	public String deleteDept(HttpServletRequest request,HttpServletResponse response){
		try{
			// 接收参数:
			String dept_id = request.getParameter("dept_id");
			// 调用业务层:
			DeptManagerService deptManagerService =(DeptManagerService)BeanFactory.getBean("deptManagerService");
			deptManagerService.deleteDept(dept_id);
			
			response.sendRedirect(request.getContextPath()+"/DeptManagerServlet?method=findAllDept");
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
		return null;
	}
	
	

}
